package com.xwj.factory;

/**
 * 简单工厂模式
 */
public class SimpleFactoryDemo {
    public static void main(String[] args) {
        PizzaStore pizzaStore = new PizzaStore(new SimplePizzaFactory());
        pizzaStore.orderPizza("clam");
    }
}

/**
 * 简单Pizza工厂
 */
class SimplePizzaFactory {
    public Pizza createPizza(String type){
        Pizza pizza = null;
        if(type.equals("cheese")){
            pizza = new CheesePizza();
        }
        else if(type.equals("clam")){
            pizza = new ClamPizza();
        }
        else if(type.equals("pepperoni")){
            pizza = new PepperoniPizza();
        }
        else if(type.equals("veggie")){
            pizza = new VeggiePizze();
        }
        return pizza;
    }
}

class PizzaStore {
    //SimplePizzaFactory的引用
    SimplePizzaFactory factory;
    public PizzaStore(SimplePizzaFactory factory){
        this.factory = factory;
    }

    public Pizza orderPizza(String type){
        Pizza pizza;
        //使用工厂对象的创建方法，而不是直接new。这里不再使用具体实例化
        pizza = factory.createPizza(type);
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
        return pizza;
    }
}

/**
 * 抽象pizza类
 */
abstract class Pizza {
    public abstract void prepare();
    public abstract void bake();
    public abstract void cut();
    public abstract void box();
}

/**
 * 具体披萨：CheesePizza.java
 */
class CheesePizza extends Pizza{
    @Override
    public void bake() {
        System.out.println("bake CheesePizza ...");
    }
    @Override
    public void box() {
        System.out.println("box CheesePizza ...");
    }
    @Override
    public void cut() {
        System.out.println("cut CheesePizza ...");
    }
    @Override
    public void prepare() {
        System.out.println("prepare CheesePizza ...");
    }
}

class ClamPizza extends Pizza{
    @Override
    public void bake() {
        System.out.println("bake ClamPizza ...");
    }
    @Override
    public void box() {
        System.out.println("box ClamPizza ...");
    }
    @Override
    public void cut() {
        System.out.println("cut ClamPizza ...");
    }
    @Override
    public void prepare() {
        System.out.println("prepare ClamPizza ...");
    }
}

class PepperoniPizza extends Pizza{
    @Override
    public void bake() {
        System.out.println("bake PepperoniPizza ...");
    }
    @Override
    public void box() {
        System.out.println("box PepperoniPizza ...");
    }
    @Override
    public void cut() {
        System.out.println("cut PepperoniPizza ...");
    }
    @Override
    public void prepare() {
        System.out.println("prepare PepperoniPizza ...");
    }
}

class VeggiePizze extends Pizza{
    @Override
    public void bake() {
        System.out.println("bake VeggiePizze ...");
    }
    @Override
    public void box() {
        System.out.println("box VeggiePizze ...");
    }
    @Override
    public void cut() {
        System.out.println("cut VeggiePizze ...");
    }
    @Override
    public void prepare() {
        System.out.println("prepare VeggiePizze ...");
    }
}